/******************************************************************************
 * FileName:    gwin_board.h
 * Desc:        板载
 *
 *
 * Author:      Lin
 * Notes:
 *
 *
 ******************************************************************************/

#ifndef _GWIN_BOARD_H_
#define _GWIN_BOARD_H_


/*-------------------------- Includes ---------------------------------------*/
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>

#include <device.h>
#include <soc.h>
#include <settings/settings.h>

#include <malloc.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <nuttx/timers/pwm.h>
#include <nuttx/timers/watchdog.h>
#include <nuttx/ioexpander/gpio.h>
#include <nuttx/analog/ioctl.h>
#include <nuttx/analog/adc.h>

//#include "miio_user_api.h"
#include "mible_mesh_api.h"
#include "mible_api.h"

#include "gwin_product_info.h"
#include "gwin_misc_api.h"


/*-------------------------- Macro Definitions ------------------------------*/
#define GWIN_DEBUG_EN                           ( 1 )

#define GWIN_WDG_TIMEOUT                        ( 30 * 1000 )

#define GWIN_DEV_BTN_CNT_MAX                    3
#define GWIN_DEV_ENCODER_CNT_MAX                2
#define GWIN_DEV_MOTOR_CNT_MAX                  2

#define GWIN_PIN_DEBUG_EN                       ( 1 )           // 手动同步 GWIN_DEBUG_EN
#if GWIN_PIN_DEBUG_EN
#define GWIN_UART0_TX_PIN_TO_UART               ( GPIO_PIN_PD7 )
#define GWIN_UART0_TX_PIN_TO_GPIO               ( GPIO_PIN_PD0 )
#else
#define GWIN_UART0_TX_PIN_TO_UART               ( GPIO_PIN_PD0 )
#define GWIN_UART0_TX_PIN_TO_GPIO               ( GPIO_PIN_PD7 )
#endif

#define GWIN_PIN_LED                            ( GPIO_PIN_PC3 )
#define GWIN_PIN_KEY                            ( GPIO_PIN_PB4 )
#define GWIN_PIN_SW_1                           ( GPIO_PIN_PA7 )
#define GWIN_PIN_SW_2                           ( GPIO_PIN_PD7 )
#define GWIN_PIN_ENCODER_1                      ( GPIO_PIN_PA4 )
#define GWIN_PIN_ENCODER_2                      ( GPIO_PIN_PB5 )
#define GWIN_PIN_MOTOR_1                        ( GPIO_PIN_PD2 )
#define GWIN_PWM_GPIO_AF_MUX_MOTOR_1            ( GPIO_AF_MUX2 )
#define GWIN_PIN_MOTOR_2                        ( GPIO_PIN_PD5 )
#define GWIN_PWM_GPIO_AF_MUX_MOTOR_2            ( GPIO_AF_MUX0 )
// board_model_mhcb11s.h 编号，注意引脚映射表
#define GWIN_GPIOx_LED                          0
#define GWIN_GPIOx_KEY                          1
#define GWIN_GPIOx_SW_1                         2
#define GWIN_GPIOx_SW_2                         3
#define GWIN_GPIOx_ENCODER_1                    4
#define GWIN_GPIOx_ENCODER_2                    5
#define GWIN_PWMx_MOTOR_1                       3
#define GWIN_PWMx_MOTOR_2                       0

#define GWIN_UART_MAX_LEN                       ( 32 )
#define GWIN_UART_RX_BYTE_TIMEOUT               ( 10 )

#define PWM_CLOCK_FRE                           ( 48 * 1000 * 1000 )    // PWM时钟主频48Mhz
#define PWM_FREQUENCY_CNT_MAX(pwm_fre)          ( PWM_CLOCK_FRE / (pwm_fre) )
#define PWM_CNT_MAX                             ( 0xFFFF + 1 )          // LIN MARK 20230420 : MHCWB11S SDK，PWM duty取值范围：0x0001~0xFFFF


#define _GWIN_DEV_DEBUG(fmt, ...)               printk("GWIN_DEBUG [] "fmt"\r\n", ##__VA_ARGS__)
#define GWIN_DEV_DEBUG(fmt, ...)                printk("GWIN_DEBUG [%s %d] "fmt"\r\n", __FUNCTION__, __LINE__, ##__VA_ARGS__)


/*-------------------------- Type Declarations ------------------------------*/

/*-------------------------- Global Variables -------------------------------*/

/*-------------------------- Local Variables --------------------------------*/

/*-------------------------- Functions Declarations -------------------------*/

/*-------------------------- Functions Definitions --------------------------*/
void gwin_board_init(void);

void gwin_watch_dog_feed(void);
uint8_t gwin_watch_dog_reboot(void);
void gwin_led_set_state(uint8_t on);
uint8_t gwin_btn_get_state(uint8_t btn_index);
uint8_t gwin_encoder_get_state(uint8_t encoder_index);
void gwin_motor_set_pwm(uint8_t motor_index, uint32_t pwm);
void gwin_uart_interrupt_set_callback(int8_t (*uart_recv_data_parse)(uint8_t *data, uint16_t len));
void gwin_uart_send(const uint8_t *data, uint16_t len);
void gwin_uart_recv_loop(void);
void gwin_ble_adv_send(const uint8_t *data, uint16_t len);
void gwin_ble_adv_stop(void);
uint8_t gwin_ble_adv_send_with_timems(const uint8_t *data, uint8_t len, uint32_t timems);


#endif // _GWIN_BOARD_H_


/*-------------------------- END OF FILE ------------------------------------*/

